#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "effect_engine/filters/impl/OldGaussianBlurFilter.h"
#include <QFileDialog>
#include <QSizePolicy>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 创建一个新的中央部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建垂直布局
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
    mainLayout->setContentsMargins(10, 10, 10, 10);

    // 将horizontalLayoutWidget添加到主布局中
    mainLayout->addWidget(ui->horizontalLayoutWidget);

    this->ee.Init();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openFileButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("打开图片"), // 对话框标题
        QString(), // 默认目录
        tr("图片文件 (*.png *.jpg *.jpeg *.bmp);;所有文件 (*.*)")); // 文件过滤器

    if (!fileName.isEmpty()) {
        image = QImage(fileName);
        if (image.isNull()) {
            // 图片加载失败
            return;
        }
        ui->label->setPixmap(QPixmap::fromImage(image));
    }
}

void MainWindow::on_gaussianBlur_clicked()
{
    ImageInfo inputImageInfo;
    ImageInfo outputImageInfo;

    // Set up input image info
    inputImageInfo.width = image.width();
    inputImageInfo.height = image.height();
    inputImageInfo.channels = 4; // RGBA
    inputImageInfo.data = const_cast<unsigned char*>(image.constBits());
    inputImageInfo.bytesPerLine = image.bytesPerLine();

    QImage processedImage(inputImageInfo.width, inputImageInfo.height, QImage::Format_ARGB32);
    void *targetAddr = processedImage.bits();

    // Set up output image info with the same dimensions
    outputImageInfo.width = inputImageInfo.width;
    outputImageInfo.height = inputImageInfo.height;
    outputImageInfo.channels = inputImageInfo.channels;
    outputImageInfo.bytesPerLine = processedImage.bytesPerLine();
    outputImageInfo.data = targetAddr;

    // Apply gaussian blur filter
    const auto filter = std::make_shared<OldGaussianBlurFilter>();
    filter->SetRadius(this->radius);
    ee.Process(inputImageInfo, outputImageInfo, filter);

    ui->label->setPixmap(QPixmap::fromImage(processedImage));
}


void MainWindow::on_radiusSlider_sliderMoved(int position)
{
    this->radius = position;
    ui->radiusLabel->setText(QString::number(position));
}

